<?php

namespace app\data\controller\shop;

use app\data\service\ExpressService;
use app\data\service\GoodsService;
use app\data\service\UserUpgradeService;
use think\admin\Controller;
use think\admin\extend\CodeExtend;

/**
 * 产品数据管理
 * Class Goods
 * @package app\data\controller\shop
 */
class Goods extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'ShopGoods';

    /**
     * 控制器初始化
     */
    protected function initialize()
    {

    }

    /**
     * 产品数据管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '产品数据管理';
        $query = $this->_query($this->table);
        // 加载对应数据
        $this->type = $this->request->get('type', 'index');
        if ($this->type === 'index') $query->where(['deleted' => 0]);
        elseif ($this->type === 'recycle') $query->where(['deleted' => 1]);
        else $this->error("无法加载 {$this->type} 数据列表！");
        // 列表排序并显示
        $query->like('name');
        $query->equal('status')->equal('cate_id')->equal('company_id')->order('sort desc,id desc')->page();
    }

    /**
     * 产品选择器
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function select()
    {
        $query = $this->_query($this->table);
        $query->equal('status')->equal('cate_id')->like('name');
        $query->where(['deleted' => 0])->order('sort desc,id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _page_filter(array &$data)
    {
        $this->companys = GoodsService::instance()->getCompanyData();
        $this->cates = GoodsService::instance()->getCateData();
        GoodsService::instance()->bindData($data, false);
    }

    /**
     * 添加产品数据
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        $this->mode = 'add';
        $this->title = '添加产品数据';
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑产品数据
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->mode = 'edit';
        $this->title = '编辑产品数据';
        $this->_form($this->table, 'form');
    }

    /**
     * 复制编辑产品
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function copy()
    {
        $this->mode = 'copy';
        $this->title = '复制编辑产品';
        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param array $data
     */
    protected function _copy_form_filter(array &$data)
    {

    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isGet()) {
            
            // 其他表单数据
            $this->companies = GoodsService::instance()->getCompanyData();
            $this->cates = GoodsService::instance()->getCateData();

        } elseif ($this->request->isPost()) {

            if (empty($data['name'])) $this->error('产品名称不能为空！');
            if (empty($data['time_number'])) $this->error('投资时间数值不能为空');
            if (empty($data['cate_id'])) $this->error('分类不能为空');

        }
    }

    /**
     * 表单结果处理
     * @param boolean $result
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_result(bool $result)
    {
        if ($result && $this->request->isPost()) {
            $this->success('产品编辑成功！', 'javascript:history.back()');
        }
    }

    /**
     * 产品上下架
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function state()
    {
        $this->_save($this->table, $this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除产品数据
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $this->_save($this->table, $this->_vali([
            'deleted.in:0,1'  => '状态值范围异常！',
            'deleted.require' => '状态值不能为空！',
        ]));
    }

}